#include <chrono>
#include <functional>
#include <thread>
#include <atomic>

class Timer {
public:
  Timer() : is_running(false) {}

  // 启动后会循环进入
  void Start(int interval, std::function<bool()> task) {
    if (is_running) return;
    is_running = true;
    time_thread = std::thread([this, interval, task]() {
      while (is_running) {
        std::this_thread::sleep_for(std::chrono::milliseconds(interval));
        is_running = task();
      }
    });
  }

  void Stop() {
    is_running = false;
    if (time_thread.joinable()) {
      time_thread.join();
    }
  }

private:
  std::atomic<bool> is_running;
  std::thread time_thread;
};
